package com.example.widget_lib.widget.progress.horbar;

import com.example.widget_lib.ResourceTable;
import com.example.widget_lib.utils.LogUtil;
import com.example.widget_lib.utils.PxUtil;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.*;
import ohos.agp.utils.*;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.global.resource.solidxml.TypedAttribute;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

public class HorProBar extends Component implements Component.DrawTask {

    private Paint bgPaint;
    private Paint progressPaint;

    private Paint tipPaint;
    private Paint textPaint;
    /**
     * 起始,结束的数量
     */
    private Paint numPaint;

    private int mWidth;
    private int mHeight;
    private int mViewHeight;
    /**
     * 进度条的宽度
     */
    private int mProgressWidth;
    /**
     * 进度
     */
    private float mProgress;

    /**
     * 当前进度
     */
    private float currentProgress;

    /**
     * 进度动画
     */
    private AnimatorValue progressAnimator;

    /**
     * 动画执行时间
     */
    private int duration = 1000;
    /**
     * 动画延时启动时间
     */
    private int startDelay = 500;

    /**
     * 进度条弧度
     */
    private int progressRound;

    /**
     * 百分比提示框画笔的宽度
     */
    private int tipPaintWidth;

    /**
     * 百分比提示框的高度
     */
    private int tipHeight;

    /**
     * 百分比提示框的宽度
     */
    private int tipWidth;

    /**
     * 画三角形的path
     */
    private Path path = new Path();
    /**
     * 三角形的高
     */
    private int triangleHeight;
    /**
     * 进度条距离提示框的高度
     */
    private int progressMarginTop;

    /**
     * 进度移动的距离
     */
    private float moveDis;

    private Rect textRect = new Rect();
    private String textString = "0";
    private String targetString = "1500";
    /**
     * 百分比文字字体大小
     */
    private int textPaintSize;

    /**
     * 进度条背景颜色
     */
    private int bgColor = 0xFFe1e5e8;
    /**
     * 进度条颜色
     */
    private int progressColor = Color.rgb(88, 181, 250);

    private int[] shaderColors = {
            Color.rgb(101, 226, 175),
            Color.rgb(88, 181, 250)
    };

    /**
     * 绘制提示框的矩形
     */
    private RectFloat rectF = new RectFloat();
    private RectFloat bgRectF = new RectFloat();
    private RectFloat progressRectF = new RectFloat();

    /**
     * 圆角矩形的圆角半径
     */
    private int roundRectRadius;

    /**
     * 进度监听回调
     */
    private ProgressListener progressListener;

    public HorProBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init();
        initPaint();
        addDrawTask(this);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    /**
     * 初始化画笔宽度及view大小
     */
    private void init() {
        progressRound = (int) PxUtil.vp2px(3);
        tipHeight = (int) PxUtil.vp2px(15);
        tipPaintWidth = (int) PxUtil.vp2px(1);
        triangleHeight = (int) PxUtil.vp2px(3);
        roundRectRadius = (int) PxUtil.vp2px(2);
        textPaintSize = (int) PxUtil.fp2px(11);

        //view真实的高度
        mViewHeight = tipHeight + tipPaintWidth + triangleHeight + progressRound + progressMarginTop;

        progressColor = Color.rgb(88, 181, 250);
        mProgressWidth = (int) PxUtil.vp2px(80);
        progressMarginTop = (int) PxUtil.vp2px(5);
        //屏幕宽高
        mWidth = PxUtil.screenWidth();
        mHeight = mViewHeight + (int) PxUtil.vp2px(10);
    }

    public void setTargetString(String target) {
        targetString = target;
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        bgPaint = getPaint(progressRound, bgColor, Paint.Style.FILL_STYLE);
        progressPaint = getPaint(progressRound, progressColor, Paint.Style.FILL_STYLE);
        tipPaint = getPaint(tipPaintWidth, progressColor, Paint.Style.FILL_STYLE);
        initTextPaint();
        tipWidth = (int) textPaint.measureText(textString + "%") + (int) PxUtil.vp2px(10);
    }

    /**
     * 初始化文字画笔
     */
    private void initTextPaint() {
        textPaint = new Paint();
        textPaint.setTextSize(textPaintSize);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextAlign(TextAlignment.CENTER);
        textPaint.setAntiAlias(true);

        numPaint = new Paint();
        numPaint.setTextSize(textPaintSize);
        numPaint.setColor(Color.BLACK);
        numPaint.setTextAlign(TextAlignment.CENTER);
        textPaint.setAntiAlias(true);
    }

    /**
     * 统一处理paint
     *
     * @param strokeWidth
     * @param color
     * @param style
     * @return  Paint
     */
    private Paint getPaint(int strokeWidth, int color, Paint.Style style) {
        Paint paint = new Paint();
        paint.setStrokeWidth(strokeWidth);
        paint.setColor(new Color(color));
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        paint.setStyle(style);
        return paint;
    }


    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (moveDis < 0) {
            currentProgress = (mProgressWidth) * mProgress / 100;
            if (currentProgress < (int) PxUtil.vp2px(2) && currentProgress != 0) {
                currentProgress = (int) PxUtil.vp2px(2);
            }
            //移动百分比提示框，只有当前进度到提示框中间位置之后开始移动，
            //当进度框移动到最右边的时候停止移动，但是进度条还可以继续移动
            //moveDis是tip框移动的距离
            if (currentProgress <= mProgressWidth) {
                moveDis = ((mWidth - mProgressWidth - tipWidth) >> 1) + currentProgress;
            } else {
                moveDis = (mWidth + mProgressWidth - tipWidth) >> 1;
            }
            if (moveDis == 0) {
                moveDis = (mWidth - mProgressWidth - tipWidth) >> 1;
            }
        }

        Point sp = new Point(tipWidth >> 1, tipHeight);
        Point ep = new Point(moveDis + (tipWidth >> 1), tipHeight);
        Point[] points = {sp, ep};
        Color[] colors = {new Color(shaderColors[0]), new Color(shaderColors[1])};
        LinearShader shader = new LinearShader(points, null, colors, Shader.TileMode.MIRROR_TILEMODE);
        progressPaint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
        drawBgProgress(canvas);
        drawProgress(canvas);
        drawTipView(canvas);
        drawText(canvas, textString);
    }

    private void drawBgProgress(Canvas canvas) {
        bgRectF.left = (mWidth - mProgressWidth) >> 1;
        bgRectF.top = tipHeight + triangleHeight;
        bgRectF.right = (mWidth + mProgressWidth) >> 1;
        bgRectF.bottom = tipHeight + triangleHeight + progressMarginTop;
        canvas.drawRoundRect(bgRectF, progressRound, progressRound, bgPaint);
    }

    private void drawProgress(Canvas canvas) {
        progressRectF.left = (mWidth - mProgressWidth) >> 1;
        progressRectF.top = tipHeight + triangleHeight;
        if (currentProgress <= mProgressWidth) {
            progressRectF.right = currentProgress + ((mWidth - mProgressWidth) >> 1);
        } else {
            progressRectF.right = (mWidth + mProgressWidth) >> 1;
        }
        progressRectF.bottom = tipHeight + triangleHeight + progressMarginTop;
        canvas.drawRoundRect(progressRectF, progressRound, progressRound, progressPaint);
    }

    /**
     * 绘制进度上边提示百分比的view
     *
     * @param canvas
     */
    private void drawTipView(Canvas canvas) {
        drawRoundRect(canvas);
        drawTriangle(canvas);
    }

    /**
     * 绘制圆角矩形
     *
     * @param canvas
     */
    private void drawRoundRect(Canvas canvas) {
        rectF.left = moveDis;
        rectF.top = 0;
        rectF.right = tipWidth + moveDis;
        rectF.bottom = tipHeight;
        canvas.drawRoundRect(rectF, roundRectRadius, roundRectRadius, tipPaint);
    }

    /**
     * 绘制三角形
     *
     * @param canvas 画布
     */
    private void drawTriangle(Canvas canvas) {
        path.moveTo((tipWidth >> 1) - triangleHeight + moveDis, tipHeight);
        path.lineTo((tipWidth >> 1) + moveDis, tipHeight + triangleHeight);
        path.lineTo((tipWidth >> 1) + triangleHeight + moveDis, tipHeight);
        canvas.drawPath(path, tipPaint);
        path.reset();
    }

    /**
     * 绘制文字
     *
     * @param canvas 画布
     * @param textString text
     */
    private void drawText(Canvas canvas, String textString) {
        textRect.left = (int) moveDis;
        textRect.top = 0;
        textRect.right = (int) (tipWidth + moveDis);
        textRect.bottom = tipHeight;

        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        int baseline = (int) ((textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2);
        //文字绘制到整个布局的中心位置
        canvas.drawText(textPaint, textString + "%", textRect.getCenterX(), baseline);
    }

    /**
     * 实时显示进度
     *
     * @param progress 进度
     */
    public void setCurrentProgress(float progress) {
        textString = PxUtil.formatNum(progress);
        tipWidth = (int) (textPaint.measureText(textString + "%") + PxUtil.vp2px(10));
        mProgress = progress;
        currentProgress = (mProgressWidth) * progress / 100;
        if (currentProgress < PxUtil.vp2px(2) && currentProgress != 0) {
            currentProgress = PxUtil.vp2px(2);
        }
        if (mWidth == 0) {
            moveDis = -1;
        } else {
            if (currentProgress <= (mProgressWidth)) {
                moveDis = ((mWidth - mProgressWidth - tipWidth) >> 1) + currentProgress;
            } else {
                moveDis = (mWidth + mProgressWidth - tipWidth) >> 1;
            }
            if (moveDis == 0) {
                moveDis = (mWidth - mProgressWidth - tipWidth) >> 1;
            }
        }
        invalidate();
    }

    /**
     * 回调接口
     */
    public interface ProgressListener {
        void currentProgressListener(float currentProgress);
    }
}
